package com.polaris.live.popup.userprofile

import android.content.Context
import android.view.View
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupUserProfileBinding
import com.polaris.live.interface_bag.PAGViewListener
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.net.liveService
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.toUserBasicVo
import com.polaris.live.resp.go_resp.LiveFollowBean
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.photo.PicturePreview
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.widget.pag.AddFollowView
import org.libpag.PAGView

/**
 * 用户资料弹窗  公共
 *
 * @author Created by 半仙 on 2023/9/27/027 17:44
 */
class UserProfilePopup(
    context: Context,
    private val viewModel: BaseViewModel,
    private val infoOtherResp: InfoOtherResp,
    private val page: Int,
    private val objectId: Long?,
    private val goneBottom: Boolean = false,
    private val onMessageClickListener: OnClickListener? = null,
    private val onFollowerClickListener: (() -> Unit)? = null,
    private val onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    private val onGiftClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
) : BaseLiveBottomPopupView<PopupUserProfileBinding>(
    context,
    { PopupUserProfileBinding.bind(it) }
) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_user_profile
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupUserProfileBinding.bind(popupImplView)
        mBinding.apply {
            /**
             * 主播不是自己显示ban直播间消息
             */
            if (infoOtherResp.userId != UserManager.getUserId()) {
                if (infoOtherResp.relation?.followed == true) {
                    addFollower.visibility = View.GONE
                } else {
                    addFollower.visibility = View.VISIBLE
                }
            } else {
                icMoreCard.visibility = View.GONE
                goMessage.visibility = View.GONE
                addFollower.visibility = View.GONE
            }
            baseView.initView(infoOtherResp, page, true, {
                toCall()
            }, { data, view, position ->
                PicturePreview.with(activity)
                    .byView(view)
                    .setImageUrlList(data)
                    .setIndex(position)
                    .setUserInfo(infoOtherResp)
                    .go()
            }, {
                val lp = mBinding.topLl.layoutParams as MarginLayoutParams
                lp.topMargin = it
                mBinding.topLl.layoutParams = lp
            }, {
                if (LiveStateManager.isLiving() || LiveStateManager.isCalling()) {
                    return@initView
                }
                Router.instance.setPath(RoutePath.USER_SPACE_ACTIVITY)
                    .withInt("page", page)
                    .withAny("userBasic", infoOtherResp.toUserBasicVo())
                    .withLong("userId", infoOtherResp.userId).navigation()
            })

            //判断是否需要展示拨打按钮
            if (baseView.isShowCall(infoOtherResp)) {//不展示拨打按钮
                llBottom.visibility = if (goneBottom) GONE else VISIBLE
            } else {//需要展示拨打按钮
//                if (infoOtherResp.live?.living == false) {
//                    llBottom3.visibility = View.VISIBLE
//                    if (infoOtherResp.call?.actualPrice == 0L) {
//                        tvCallFree.visibility = View.VISIBLE
//                    } else {
//                        llCallBottom.visibility = View.VISIBLE
//                    }
//                    callCoin2.text = "${infoOtherResp.call?.actualPrice}"
//                    ivPlay2.apply {
//                        path = PagConst.USER_PROFILE_CALL_PAG
//                        setRepeatCount(-1)
//                        play()
//                    }
//                } else {//对方在直播的情况
//                    if (infoOtherResp.call?.actualPrice == 0L) {
//                        freeMatch.visibility = View.VISIBLE
//                    } else {
//                        callCoin.text = "${infoOtherResp.call?.actualPrice}"
//                        goCall.visibility = View.VISIBLE
//                        ivPlay.apply {
//                            path = PagConst.USER_PROFILE_CALL_PAG
//                            setRepeatCount(-1)
//                            play()
//                        }
//                    }
//                }
                llBottom2.visibility = View.VISIBLE

            }

            addFollower.apply {
                setPathType(AddFollowView.LIVE_ADD_FOLLOWE_ALPHA)
                addListener(object : PAGViewListener {
                    override fun onAnimationEnd(p0: PAGView?) {
                        super.onAnimationEnd(p0)
                        addFollower.visibility = View.INVISIBLE
                    }
                })
                setRepeatCount(1)
            }
            addFollower.setOnSingleClickListener {
                objectId?.let {
                    viewModel.request(
                        { liveService.liveFollow(LiveFollowBean(it, infoOtherResp.userId)) },
                        {
                            onFollowerClickListener?.invoke()
                            mBinding.addFollower.play()
                            ToastGravity.showGravity(com.polaris.live.translation.R.string.follower_succeed)
                        })
                }
            }
            goMessage.setOnSingleClickListener {
                toMessage(it)
            }
            llChat.setOnSingleClickListener {
                toMessage(it)
            }
            llChat2.setOnSingleClickListener {
                toMessage(it)
            }
            goCall2.setOnSingleClickListener {
                toCall()
            }
            goCall.setOnSingleClickListener {
                toCall()
            }
            freeMatch.setOnSingleClickListener {
                toCall()
            }
            llGift.setOnSingleClickListener {
                onGiftClickListener?.invoke(infoOtherResp)
                dismiss()
            }
            icMoreCard.setOnSingleClickListener {
                onSelectMoreCardListener?.invoke(infoOtherResp)
                dismiss()
            }
        }
    }

    private fun toCall() {
        StartCallProxy.builder(this@UserProfilePopup, context).startCall(
            viewModel,
            UserConst.From.LIVE,
            UserConst.LastFrom.LIVE_DETAIL,
            LiveConst.SenderFrom.MANUAL,
            infoOtherResp.userId,
            infoOtherResp.toUserBasicVo(),
            {
                dismiss()
            })
    }

    private fun toMessage(it: View) {
        if (onMessageClickListener == null) {
            Router.instance.setPath(RoutePath.MESSAGE_ACTIVITY)
                .withInt("page", page)
                .withString("nickname", infoOtherResp.nickname)
                .withString("avatar", infoOtherResp.avatar)
                .apply {
                    infoOtherResp.decoration?.avatarFrame?.also { af ->
                        withString("avatarFrame", af)
                    }
                    infoOtherResp.decoration?.chatBubble?.also { cb ->
                        withString("chatBubble", cb)
                    }
                }
                .withLong("userId", infoOtherResp.userId)
                .navigation()
        } else {
            onMessageClickListener.onClick(it)
        }
        dismiss()
    }

    override fun getContentView(): View = mBinding.bottom
}